<?php

/**
 * This is the model class for table "log".
 *
 * The followings are the available columns in table 'log':
 * @property integer $id
 * @property string $for_model
 * @property integer $foreign_key
 * @property string $log
 * @property integer $date
 * @property string $filed
 */
class Report extends ActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return Log the static model class
	 */
    public $cargo_id;
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'report';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('type, title, attn', 'length', 'max'=>50),
			array('remark', 'length', 'max'=>125),
                        array('date','type','type'=>'date','dateFormat'=>'dd/MM/yyyy'),
                    array('cargo_id','exist','className'=>'cargo','attributeName'=>'id','on'=>'addcargo'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, for_model, foreign_key, log, date, filed', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
                    'cargo'=>array(self::MANY_MANY,'cargo','report_item(report_id,cargo_id)'),
                    'cargoCount'=>array(self::STAT, 'cargo', 'report_item(report_id,cargo_id)'),
                    'reportValue'=>array(self::STAT,'cargo','report_item(report_id,cargo_id)','select'=>'sum(amt)'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			
		);
	}

        public function beforeSave() {
            if (is_numeric($this->date))
                return parent::beforeSave();

            $_date = explode("/", $this->date);
            $this->date = mktime(0, 0, 0, $_date[1], $_date[0], $_date[2]);

            return parent::beforeSave();
        }
        public function addCargo(){
            if(!$this->validate(array('cargo_id')))
                return false;
            $sql = "select * from report_item where report_id=".$this->id." and cargo_id=".$this->cargo_id;
            $command = Yii::app()->db->createCommand($sql);
            if($command->execute())
                    return true;
            $sql = "insert into report_item (report_id, cargo_id) values (".$this->id.",".$this->cargo_id.")";
            $command = Yii::app()->db->createCommand($sql);
            if($command->execute())
                return true;
            else
                return false;
        }

        public function removeCargo() {
            $sql = 'delete from report_item where cargo_id='.$this->cargo_id.',report_id'.$this->id;
            $command = Yii::app()->db->createCommand($sql);
            if($command->execute())
                return true;
            else
                return false;
        }

        public function listCargo(){
            $sql = "select * from report_item where report_id=".$this->id;
            $command=Yii::app()->db->createCommand($sql);
            $reader = $command->query();
            $criteria = new CDbCriteria;
            while($row = $reader->read()) {
                $criteria->addCondition('id='.$row['cargo_id'], 'or');
            }
            $criteria->addCondition('id=0','or');
            return new CActiveDataProvider('cargo',array(
               'criteria'=>$criteria,
            ));

        }

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));
	}
}